﻿package mortal.game.view.wizard
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.guide.navbar.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.wizard.data.*;
    import mortal.game.view.wizard.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WizardController extends Controller
    {
        private var _wizardModule:WizardNewModule;
        private var _wizardProxy:WizardProxy;
        private var _wizardCache:WizardCache;
        private var cdData:ICDData;
        private var num:int;
        private var _lastTime:Number = 0;

        public function WizardController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._wizardProxy = GameProxy.wizard;
            this._wizardCache = Cache.instance.wizard;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._wizardModule == null)
            {
                this._wizardModule = new WizardNewModule();
                this._wizardModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._wizardModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._wizardModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.WizardAcupointLevelUp, this.refreshWizard);
            NetDispatcher.addCmdListener(ServerCommand.WizardLevelUp, this.refreshWizard);
            NetDispatcher.addCmdListener(ServerCommand.WizardWinOpen, this.openWin);
            NetDispatcher.addCmdListener(ServerCommand.VitalEnergyUpdate, this.showUpdateIcon);
            NetDispatcher.addCmdListener(ServerCommand.WizardInfoUpde, this.showUpdateIcon);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.wizardItemAdd);
            Dispatcher.addEventListener(EventName.WizardShowModule, this.showWizardModuleHandler);
            Dispatcher.addEventListener(EventName.WizardItemUse, this.wizardItemUse);
            Dispatcher.addEventListener(EventName.WizardUpgradeSoul, this.upgradeSoul);
            Dispatcher.addEventListener(EventName.WizardLevelUpSoul, this.levelUpSoul);
            Dispatcher.addEventListener(EventName.WizardSelectFight, this.wizardSelectFight);
            Dispatcher.addEventListener(EventName.WizardUseXpSkill, this.useXpSkill);
            Dispatcher.addEventListener(EventName.WizardUseUpgradeItemInPack, this.useUpgradeItemInPack);
            Dispatcher.addEventListener(EventName.WizardEquip, this.wizardEquipHandler);
            Dispatcher.addEventListener(EventName.WizardUpgradeSkill, this.wizardUpgradeSkill);
            return;
        }// end function

        private function showWizardModuleHandler(param1 = null) : void
        {
            this.openWin(1);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            if (WizardIcon.isViewShow)
            {
                WizardIcon.instance.hide();
            }
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateItemHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.WizardEffectPlay, this.wizardEffectPlay);
            NetDispatcher.addCmdListener(ServerCommand.UpdateWizardEquip, this.wizardEquipUpdate);
            NetDispatcher.addCmdListener(ServerCommand.WizardSkillUpdate, this.wizardSkillUpdate);
            NetDispatcher.addCmdListener(ServerCommand.WizardActive, this.wizardActive);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateItemHandler);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.WizardEffectPlay, this.wizardEffectPlay);
            NetDispatcher.removeCmdListener(ServerCommand.UpdateWizardEquip, this.wizardEquipUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.WizardSkillUpdate, this.wizardSkillUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.WizardActive, this.wizardActive);
            return;
        }// end function

        private function updateRedPointTime(event:DataEvent) : void
        {
            this.cdData = Cache.instance.cd.registerCDData(CDDataType.publicCD, "WizardRedPoint", this.cdData);
            this.cdData.totalTime = 7200000;
            this.cdData.beginTime = 0;
            this.isShowRedPoint();
            return;
        }// end function

        private function isShowRedPoint() : void
        {
            var _loc_1:* = WizardUtil.canOperWizardData;
            if (_loc_1.data is WizardData)
            {
                NavbarButtonUIRow.instance.showHideFilter(ModuleType.Wizard, true);
            }
            else
            {
                NavbarButtonUIRow.instance.showHideFilter(ModuleType.Wizard, false);
                this.cdData.startCoolDown();
                this.cdData.addFinishCallback(this.isShowRedPoint);
            }
            return;
        }// end function

        private function getPlayerSoul() : void
        {
            return;
        }// end function

        private function upgradeSoul(event:DataEvent) : void
        {
            var _loc_2:* = event.data as WizardData;
            if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitVitalEnergy, _loc_2.tSprite.vitalenergy))
            {
                MsgManager.showMouseMsg(Language.getString(30412), MsgRollTipsType.msgRollTips3);
                if (GuideController.currentGuideItemWin == ModuleType.Wizard)
                {
                    view.hide();
                    GuideController.currentGuideItemWin = null;
                }
                else
                {
                    this._wizardModule.showFilter(WizardInfoPanel.consumeBox2);
                }
                return;
            }
            else if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitCoinBind, _loc_2.tSprite.coin))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowBindCoinPackWin));
                return;
            }
            this._wizardProxy.upgradeMeridian(_loc_2.code);
            return;
        }// end function

        private function refreshWizard(param1:Object) : void
        {
            this._wizardModule.refreshSprite();
            this._wizardModule.updateAttrData();
            return;
        }// end function

        private function levelUpSoul(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._wizardProxy.spriteEvolution(_loc_2.code);
            return;
        }// end function

        private function updateItemHandler(param1:Object) : void
        {
            if (this._wizardModule)
            {
                this._wizardModule.updateItemHandler();
            }
            return;
        }// end function

        private function updateMoney(param1:Object) : void
        {
            this._wizardModule.updateMoney();
            return;
        }// end function

        private function openWin(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (param1 is int)
            {
                _loc_2 = (param1 as int) - 1;
            }
            else if (Cache.instance.wizard.fightWizardCode)
            {
                _loc_2 = Cache.instance.wizard.fightWizardCode - 1;
            }
            else
            {
                _loc_2 = Cache.instance.wizard.fightWizardCode;
            }
            if (!GameController.wizard.isViewShow)
            {
                this.view.show();
                this._wizardModule.showFilter(WizardInfoPanel.upgradeBtn);
            }
            this._wizardModule.showSelectWizard(_loc_2);
            return;
        }// end function

        private function wizardSelectFight(event:DataEvent) : void
        {
            this._wizardProxy.changeSprite(event.data as int);
            return;
        }// end function

        private function useXpSkill(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_4:WizardData = null;
            var _loc_5:SSkill = null;
            var _loc_6:AutoFightAI = null;
            if (!curMapCanUseXp())
            {
                return MsgManager.showRollTipsMsg(Language.getString(35619));
            }
            if (Cache.instance.wizard.fightWizardCode == 0)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35620), MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.role.roleInfo.anger < GameConst.MaxAnger)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35621), MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.role.entityInfo.life == 0)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35622), MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeChangeBody))
            {
                MsgManager.showRollTipsMsg(Language.getString(35623));
                return;
            }
            if (event.data)
            {
                _loc_2 = event.data as int;
            }
            else
            {
                _loc_4 = Cache.instance.wizard.fightWizardData;
                _loc_2 = _loc_4.tSpriteEx.xpskillId;
            }
            var _loc_3:* = new SkillInfo();
            _loc_3.tSkill = SkillConfig.instance.getInfoById(_loc_2);
            if (cache.skill.getSkillBySerialId(_loc_3.tSkill.series) == null)
            {
                _loc_5 = new SSkill();
                _loc_5.skillId = _loc_3.skillId;
                Cache.instance.skill.addPlayerSkill(_loc_5);
            }
            else
            {
                cache.skill.getSkillBySerialId(_loc_3.tSkill.series).tSkill = _loc_3.tSkill;
            }
            if (AutoFightAI.isWorking && AIManager.curAI is AutoFightAI)
            {
                _loc_6 = AIManager.curAI as AutoFightAI;
                _loc_6.useSkill(_loc_3, true);
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SkillCheckAndSkillAI, _loc_3));
            }
            return;
        }// end function

        private function useUpgradeItemInPack(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:WizardData = null;
            var _loc_5:WizardData = null;
            var _loc_2:* = Cache.instance.wizard.soulList;
            for each (_loc_5 in _loc_2)
            {
                
                if (_loc_5.isHasWizard && _loc_5.spriteInfo.AcupointIndex == 11)
                {
                    if (_loc_4)
                    {
                        if (_loc_5.spriteInfo.SpriteLevel > _loc_4.spriteInfo.SpriteLevel)
                        {
                            _loc_4 = _loc_5;
                        }
                        continue;
                    }
                    _loc_4 = _loc_5;
                }
            }
            if (_loc_4)
            {
                _loc_3 = _loc_4.spriteInfo.SpriteCode;
            }
            else
            {
                _loc_3 = 1;
            }
            this.openWin(_loc_3);
            return;
        }// end function

        private function wizardEffectPlay(param1:Object) : void
        {
            var _loc_2:* = int(param1);
            this._wizardModule.playEffect(_loc_2);
            return;
        }// end function

        private function wizardEquipHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data.putonitemData as ItemData;
            var _loc_3:* = event.data.getoffItemData as ItemData;
            this._wizardProxy.dressWizardEquip(event.data.code, _loc_2 ? (_loc_2.uid) : (""), _loc_3 ? (_loc_3.uid) : (""));
            return;
        }// end function

        private function wizardEquipUpdate(param1:Object) : void
        {
            this._wizardModule.updateEquip();
            return;
        }// end function

        private function wizardSkillUpdate(param1:Object) : void
        {
            this._wizardModule.updateSkill();
            this._wizardModule.updateCombat();
            return;
        }// end function

        private function wizardUpgradeSkill(event:DataEvent) : void
        {
            var _loc_5:ItemInfo = null;
            var _loc_2:* = event.data as WizardData;
            if (_loc_2.spriteInfo.skillCode >= WizardConfig.instance.getSkillTotalStar(_loc_2.code))
            {
                if (this.isViewShow)
                {
                    this._wizardModule.showFilter(WizardInfoPanel.starPanel);
                }
                return MsgManager.showRollTipsMsg(Language.getString(35624));
            }
            var _loc_3:* = WizardConfig.instance.getTSpriteExByCodeAndStar(_loc_2.code, (_loc_2.spriteInfo.skillCode + 1));
            var _loc_4:* = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_3.prop);
            if (_loc_4 < _loc_3.amount)
            {
                _loc_5 = ItemConfig.instance.getConfig(_loc_3.prop);
                if (this.isViewShow)
                {
                    this._wizardModule.showFilter(WizardInfoPanel.levelUpItemtxt);
                }
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35625, _loc_2.tSprite.name, _loc_3.amount) + _loc_5.name, MsgRollTipsType.msgRollTips3);
            }
            this._wizardProxy.upgradeSpriteSkill(_loc_2.code);
            return;
        }// end function

        private function wizardItemUse(event:DataEvent) : void
        {
            var _loc_6:ItemInfo = null;
            var _loc_2:* = event.data as WizardData;
            var _loc_3:* = WizardConfig.instance.getTSpriteExByCodeAndStar(_loc_2.code, 1);
            var _loc_4:* = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_3.prop);
            if (_loc_4 < _loc_3.amount)
            {
                _loc_6 = ItemConfig.instance.getConfig(_loc_3.prop);
                if (this.isViewShow)
                {
                    this._wizardModule.showFilter(WizardInfoPanel.levelUpItemtxt);
                }
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35626, _loc_2.tSprite.name, _loc_3.amount) + _loc_6.name, MsgRollTipsType.msgRollTips3);
            }
            var _loc_5:* = Cache.instance.pack.backPackCache.getItemByCode(_loc_3.prop, true);
            this._wizardProxy.activeSprint(_loc_5);
            return;
        }// end function

        private function showUpdateIcon(param1:Object = null) : void
        {
            if (Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(62))
            {
                NetDispatcher.removeCmdListener(ServerCommand.VitalEnergyUpdate, this.showUpdateIcon);
                NetDispatcher.removeCmdListener(ServerCommand.WizardInfoUpde, this.showUpdateIcon);
                return;
            }
            var _loc_2:* = ClockManager.instance.nowDate.time;
            if (_loc_2 - this._lastTime >= 60000 && WizardUtil.isEnoughToLevelUp)
            {
                WizardIcon.instance.show();
                this._lastTime = _loc_2;
            }
            return;
        }// end function

        private function wizardActive(param1:int) : void
        {
            if (GameController.wizard.isViewShow)
            {
                this._wizardModule.showSelectWizard((param1 - 1));
            }
            this._wizardModule.updateAttrData();
            return;
        }// end function

        private function wizardItemAdd(param1:Array) : void
        {
            if (GameController.autoGuide.hasGuideWizard)
            {
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.wizardItemAdd);
            }
            return;
        }// end function

        public static function curMapCanUseXp() : Boolean
        {
            if (CopyUtil.isInZZXYBattleCopy || CopyUtil.isInSixBossCopy)
            {
                return false;
            }
            return true;
        }// end function

    }
}
